package com.cfwx.db.jdbc;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * JDBC 工具方法
 * @author Administrator
 *
 */
public class JdbcUtil {
	//mysql
	public static final String CONNECTION_DRIVER = "com.mysql.jdbc.Driver";
	public static final String CONNECTION_URL = 
					"jdbc:mysql://127.0.0.1:3306/train?" +
					"useUnicode=true&characterEncoding=utf-8";
	
	
	//oracle
	public static final String driverClassName="oracle.jdbc.driver.OracleDriver";
	public static final String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";		
	
	
	/**
	 * 取得一个数据库的连接
	 * @return Connection
	 */
	public static Connection getConnection() {
		Connection connection = null;
		try {
			Driver driver = (Driver) Class.forName(driverClassName).newInstance();
			connection = DriverManager.getConnection(url, "pmsuserscj", "pmsuser");
			return connection;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			
		}
	}
	
	/**
	 * 关闭一个连接
	 * @param connection Connection
	 */
	public static void closeConnection(Connection connection) {
		try {
			if(null != connection && !connection.isClosed()) {
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
